package com.cx.util.czw;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class MsgUtil {

    public static int sendMsg(String Phone, String randomCode) {
        String accessKeyId = "LTAI4FzuUvCPe97SPBGyJNzu";
        String accessSecret = "qy7G2BmOHoaQAeOAuy9dPZAeND5zpP";

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//云服务(不能修改)
        request.setSysVersion("2017-05-25");//版本号(不能修改)
        request.setSysAction("SendSms"); // 发送方式(不能修改)
        request.putQueryParameter("RegionId", "cn-hangzhou");
        // 发送的手机号（多个手机号可以用逗号隔开）
        request.putQueryParameter("PhoneNumbers", Phone);
        request.putQueryParameter("SignName", "瞬达");
        // 3.请求模板(短信模板)
        request.putQueryParameter("TemplateCode", "SMS_205884743");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+randomCode+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            // 返回
            String result = response.getData();
            System.out.println();
            System.out.println(response.getData());

            // 转json
            JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
            // 请求成功
            if ("OK".equals(jsonObject.getString("Code"))) {
                return 1;
            } else {
                // 失败
                return 2;
            }

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return 2;
    }
}
